﻿using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Xml;
using Blader.Entity;

namespace Blader.Config
{
    public class ConfigReader
    {
        private static ConfigReader instance = new ConfigReader();

        public static ConfigReader GetInstance()
        {
            return instance;
        }

        public string GetBaseDirectory()
        {
            return Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
        }

        public List<ProjectItem> LoadProjects()
        {
            List<ProjectItem> projectItems = new List<ProjectItem>();

            string projectXmlFile = Path.Combine(GetBaseDirectory(), "Projects.xml");

            XmlDocument document = new XmlDocument();

            document.Load(projectXmlFile);

            XmlNodeList nodeList = document.SelectNodes("//ProjectItem");

            foreach (XmlNode xmlNode in nodeList)
            {
                ProjectItem projectItem = new ProjectItem()
                                            {
                                                Label = xmlNode.SelectSingleNode("Label").InnerText,
                                                Path = xmlNode.SelectSingleNode("Path").InnerText,
                                                Active = bool.Parse(xmlNode.SelectSingleNode("Active").InnerText),
                                            };
                projectItems.Add(projectItem);
            }

            return projectItems;
        }
    }
}
